package com.study.dao.impl;

import com.study.bean.entity.DormitoryAdmin;
import com.study.dao.AdminDao;
import com.study.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AdminDaoImpl implements AdminDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public DormitoryAdmin login(String username, String password) {
        String sql = "select * from dormitory_admins where username = ? and password = ?";
        DormitoryAdmin dormitoryAdmin = template.queryForObject(sql, new BeanPropertyRowMapper<>(DormitoryAdmin.class), username, password);
        return dormitoryAdmin;
    }

    @Override
    public int updatePwd(DormitoryAdmin admin, String newPass) {

        String sql = "update  dormitory_admins set password = ? where admin_id = ? ";
        int update = template.update(sql, newPass, admin.getAdminId());
        return update;
    }

    @Override
    public DormitoryAdmin updateAvatar(Integer adminId, String fileName) {

        String sql = "update dormitory_admins set avatar = ? where admin_id = ?";

        template.update(sql, fileName, adminId);
        sql = "select * from dormitory_admins where admin_id = ?";
        DormitoryAdmin dormitoryAdmin = template.queryForObject(sql, new BeanPropertyRowMapper<>(DormitoryAdmin.class), adminId);
        return dormitoryAdmin;
    }
}
